﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastracture.HttpClient;
using ConfApp.Infrastructure;
using ConfApp.Models;
using Microsoft.Phone.Reactive;

namespace ConfApp.Services.Podcasts
{
    public class PodcastServiceClient : IPodcastServiceClient
    {
        private const string Query = "http://www.mssinergija.net/sr/sinergija11/Multimedija/_layouts/listfeed.aspx?List={41220A15-AA96-4E98-84D3-10BDF85DD51B}";
        public IObservable<PodcastBatch> GetPodcasts()
        {
            var uri = new Uri(Query);
            return
                HttpClient
                    .RequestTo(uri)
                    .GetXml()
                    .Select(xml=>
                                    new PodcastBatch()
                                        {
                                            Podcasts = xml.Root.Element("channel").Descendants("item")
                                                .Select(
                                                    item=> new Podcast()
                                                        {
                                                            Title = item.Element("title").Value,
                                                            Author = item.Element("author").Value,
                                                            Url = item.Element("link").Value,
                                                            Details = item.Element("description").Value,
                                                            Date = DateTime.Parse(item.Element("pubDate").Value)
                                                        }).ToList()
                                        });
        }
    }
}
